#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<vector<int>> fourSum(vector<int>& nums, int target) {
    int n = nums.size();
    sort(nums.begin(), nums.end());
    vector<vector<int>> ret;
    for (int i = 0; i < n;) {
        for (int j = i + 1; j < n;) {
            int sum = -(target - nums[i] - nums[j]);
            int left = j + 1;
            int right = n - 1;
            while (left < right) {
                if (nums[left] + nums[right] > sum)
                    right--;
                else if (nums[left] + nums[right] < sum)
                    left++;
                else {
                    ret.push_back({ nums[left++], nums[right--], nums[j] });
                    while (left < right && nums[left] == nums[left - 1])
                        left++;
                    while (left < right && nums[right] == nums[right + 1])
                        right--;
                }
            }
            j++;
            while (j < n && nums[j - 1] == nums[j])
                j++;
        }
        i++;
        while (i < n && nums[i - 1] == nums[i])
            i++;
    }
    return ret;
}
int main()
{
    vector<int> v = { 1,0,-1,0,-2,2 };
    fourSum(v, 0);
}